package task

import (
	"gorm.io/gorm"

	"torler.com/qingyue/common/types"
)

func GetAllTaskNotFinished(db *gorm.DB) []*TaskRunInfo {
	var tasks []*TaskRunInfo
	db.Where("state = ?", types.TaskStatusInit).Or("state = ?", types.TaskStatusRunning).Or("state = ?", types.TaskStatusQueuing).Find(&tasks)
	return tasks
}

func saveTaskRecord(db *gorm.DB, record *TaskRunInfo) error {

	return db.Save(record).Error
}

// 获取任务的所有运行记录
func GetTaskRecordsByTaskId(db *gorm.DB, taskId string) ([]*TaskRunInfo, error) {
	var records []*TaskRunInfo
	err := db.Where("tid = ?", taskId).Order("created_at DESC").Find(&records).Error
	return records, err
}
